<?php
/*
* $Author ：PHPYUN开发团队
*
* 官网: http://www.phpyun.com
*
* 版权所有 2009-2018 宿迁鑫潮信息技术有限公司，并保留所有权利。
*
* 软件声明：未经授权前提下，不得用于商业运营、二次开发以及任何形式的再次发布。
 */
class model{
    const MODEL_INSERT          =   1; 
    const MODEL_UPDATE          =   2; 
    const MODEL_BOTH            =   3; 
    const MUST_VALIDATE         =   1;
    const EXISTS_VALIDATE       =   0; 
    const VALUE_VALIDATE        =   2; 

    protected $config           =   null;
    protected $db               =   null;
    protected $pk               =   'id';
    protected $autoinc          =   false;
    protected $def      =   null;
    protected $name             =   '';
    protected $coding             =   '';
    protected $dbName           =   '';
    protected $connection       =   '';
    protected $tableName        =   '';
    protected $trueTableName    =   '';
    protected $error            =   '';
    protected $fields           =   array();
    protected $data             =   array();
    protected $options          =   array();
    protected $_validate        =   array();
    protected $_auto            =   array();
    protected $_map             =   array(); 
    protected $_scope           =   array(); 
    protected $autoCheckFields  =   true;
    protected $patchValidate    =   false;
    protected $methods          =   array('order','alias','having','group','lock','distinct','auto','filter','validate','result','token');
    protected $sitetable=array('ad','ad_order','admin_announcement','zhaopinhui','zhaopinhui_com','zhaopinhui_pic','gaoduan','gaoduan_com','gaoduan_pic','news_base','news_content','resume_expect','member','company_job','resume','user_entrust','user_entrust_record','down_resume','look_resume','company','company_cert','company_msg','company_news','company_order','company_pay','company_product','once_job','resume_tiny','userid_msg','userid_job','look_job','admin_link','px_subject','px_train_news','px_teacher','lt_info','lt_job','report','invoice_record','company_statis','px_train','partjob','hotjob','px_baoming','px_zixun');


	protected $admindir;
	protected $siteadmindir;

	protected $logininfo;

	public function __construct($db,$def,$logininfo){
		global $coding,$config,$adminDir,$siteAdminDir;

		$this->db = $db;
		$this->tp = '';
		$this->def = $def;
		$this->md = $coding;
        $this->config = $config;
		$this->uid = $logininfo['uid'];
		$this->username = $logininfo['username'];
		$this->usertype = $logininfo['usertype'];
		$this->admindir = $adminDir;
		$this->siteadmindir = $siteAdminDir;
		
		if(!($this->config['sy_wapdomain'])){
			
			$this->config['sy_wapdomain'] = $this->config['sy_weburl'].'/'.$this->config['sy_wapdir'];
		}else{
			
			if($config['sy_wapssl']=='1'){
				$protocol = 'https://';
			}else{
				$protocol = 'http://';
			}

			if(strpos($this->config['sy_wapdomain'],'http://')===false && strpos($this->config['sy_wapdomain'],'https://')===false)
			{
				$this->config['sy_wapdomain'] = $protocol.$this->config['sy_wapdomain'];
			}
		}
		if($adminDir||!$siteAdminDir){
			$this->sitetable = array();
		} 
	}

    public function __set($name,$value) {
       
        $this->data[$name]  =   $value;
    }

    public function __get($name) {
        return isset($this->data[$name])?$this->data[$name]:null;
    }

    public function __isset($name) {
        return isset($this->data[$name]);
    }

    public function __unset($name) {
        unset($this->data[$name]);
    }

   
    protected function _initialize() {}

   
    function insert_into($table,$data=array()){

		
		$value=array();
		
		$this->db->connect();
        include(PLUS_PATH.'dbstruct.cache.php');
        $TableFullName=$this->def.$table;
        if(is_array($$TableFullName)){
            $fields=array_keys($$TableFullName);
        }else{
			return false;
		}

		if(is_array($fields)){
		
			if(is_array($data)){
				foreach($data as $key=>$v){
					if(in_array($key,$fields)){
						$v = $this->FilterStr($v);
						$value[]="`".$key."`='".$this->db->escape_string($v)."'";
					}
				}
			}
		}
		$value=@implode(",",$value);
		return $this->DB_insert_once($table,$value);
	}
	function update_once($table,$data=array(),$w=''){ 
		
		$this->db->connect();
		$value=array();
		$where=array();
		include(PLUS_PATH.'dbstruct.cache.php');
        $TableFullName=$this->def.$table;
        if(is_array($$TableFullName)){
            $fields=array_keys($$TableFullName);
        }else{
			return false;
		}

		if(is_array($fields)){
			

			if(is_array($data)){
				foreach($data as $key=>$v){
					if(in_array($key,$fields)){
						$v = $this->FilterStr($v);
						$value[]="`".$key."`='".$this->db->escape_string($v)."'";
					}
				}
			}

			if(is_array($w)){
				foreach($w as $key=>$v){
					if(in_array($key,$fields)){
						$v = $this->FilterStr($v);
						$where[]="`".$key."`='".$this->db->escape_string($v)."'";
					}
				}
				$where=@implode(" and ",$where);
			}else{
				$where = $w;
			}
			$value=@implode(",",$value);
			return $this->DB_update_all($table,$value,$where);
		}
	}
	function FilterStr($str){
		$str = stripslashes($str);
		return $str;
	}
	function Memcache_set($name,$value=""){

		
	}
	function DB_select_num($tablename, $where = 1, $select = "*",$tablename2='',$special=''){
		if(!$this->checkTableName($tablename)){
			
			return false;
		}
		$cachename=$tablename.$where;
		if(!$return=$this->Memcache_set($cachename)){//获取是否存在memcache
			if($tablename2){
				if($this->siteadmindir && $special==''){
				   if(in_array($tablename,$this->sitetable) && is_numeric($this->config['did'])){
					  $Where='a.`did`='.$this->config['did']."' and ".$Where;
				   }
				   if(in_array($tablename2,$this->sitetable) && is_numeric($this->config['did'])){
					  $Where='b.`did`='.$this->config['did']."' and ".$Where;
				   }
				}

				$SQL = "SELECT count($select) as num FROM " . $this->def . $tablename . " as a," . $this->def . $tablename2 . " as b  WHERE $where";
			}else{

				if($this->siteadmindir && $special==''){

					$where = $this->site_fetchsql($where,$tablename);
				}
				$SQL = "SELECT count($select) as num FROM " . $this->def . $tablename . " WHERE $where";
			}
			$query = $this->db->query($SQL);
			while($row=$this->db->fetch_array($query)){$return=$row['num'];}
			$this->Memcache_set($cachename,$return);//设置memcache
		}
		if($return<1){$return='0';}
		return $return;
	}

	function DB_select_query($tablename, $where = 1, $select = "*",$special='') {
		if(!$this->checkTableName($tablename)){
			
			return false;
		}
		if($this->siteadmindir){

			$where = $this->site_fetchsql($where,$tablename);
		}
	    $SQL = "SELECT $select FROM " . $this->def . $tablename . " WHERE $where";
		$query=$this->db->query($SQL);
		return $query;
	}
	
	function DB_select_all($tablename, $where = 1, $select = "*",$special='') {
		if(!$this->checkTableName($tablename)){
			
			return false;
		}
		
		$cachename=$tablename.$where;
		if(!$row_return=$this->Memcache_set($cachename)){//获取是否存在memcache
			$row_return=array();  
			if($this->siteadmindir&&$special==''){

					$where = $this->site_fetchsql($where,$tablename);
			}  
			$SQL = "SELECT $select FROM `" . $this->def . $tablename . "` WHERE ".$where;

			$query=$this->db->query($SQL);
            while($row=$this->db->fetch_array($query)){$row_return[]=$row;}
            $this->Memcache_set($cachename,$row_return);//设置memcache
		}
        return $row_return;
	}
	
	function DB_select_alls($tablename1,$tablename2, $where = 1, $select = "*") {
		if(!$this->checkTableName($tablename1)){
			
			return false;
		}
		if(!$this->checkTableName($tablename2)){
			
			return false;
		}
		$cachename=$tablename1.$tablename2.$where;
		if(!$row_return=$this->Memcache_set($cachename)){


			if($this->siteadmindir){

				 if(in_array($tablename1,$this->sitetable) && is_numeric($this->config['did'])){
						$where='a.`did`='.$this->config['did']." and ".$where;
				 }
				 if(in_array($tablename2,$this->sitetable) && is_numeric($this->config['did'])){


						$where='b.`did`='.$this->config['did']." and ".$where;
				 }
			}
			$SQL = "SELECT $select FROM " . $this->def . $tablename1. " as a," . $this->def . $tablename2 . " as b WHERE $where";

			$query=$this->db->query($SQL);
            while($row=$this->db->fetch_array($query)){$row_return[]=$row;}
            $this->Memcache_set($cachename,$row_return);
		}

        return $row_return;
	}
	
	function DB_insert_once($tablename, $value){
		if(!$this->checkTableName($tablename)){
			
			return false;
		}
		if(in_array($tablename,$this->sitetable) && strpos($value,'`did`')===false){
			$value.=",`did`='".$this->config['did']."'";
		} 
		$SQL = "INSERT INTO `" . $this->def . $tablename . "` SET ".$value;
		$this->db->query("set sql_mode=''");
		$this->db->query($SQL);
		$nid= $this->db->insert_id();
		return $nid;
	}

  function DB_insert_multi($tablename, $valueArr)
  {
  
	if(!$this->checkTableName($tablename)){
			
			return false;
	}
    include(PLUS_PATH.'dbstruct.cache.php');
    $TableFullName=$this->def.$tablename;
    if(is_array($$TableFullName)){
        $fieldsArr = array_keys($$TableFullName);
    }
    if($fieldsArr){
      $fields = array();
      foreach($valueArr[0] as $f => $v){
        if(in_array($f, $fieldsArr)){
          $fields [] = "`$f`";
        }
      }
      $fields = implode(',', $fields);
      
      $values = array();
      foreach($valueArr as $r){
        $arr = array();
        foreach($r as $k => $v){
          if(in_array($k, $fieldsArr)){
            $arr [] = "'" . $this->db->escape_string($v) . "'";
          }
        }
        $values [] = '(' . implode(',', $arr) . ')';
      }
      $values = implode(',', $values);
      $SQL = "INSERT INTO `{$this->def}{$tablename}` ($fields) VALUES $values";
      $this->db->query("set sql_mode=''");
      $this->db->query($SQL);
    }
    
  }
	
	function DB_update_all($tablename, $value, $where = 1,$pecial=''){
		if(!$this->checkTableName($tablename)){
			
			return false;
		}
		if($pecial!=$tablename){

			$where =$this->site_fetchsql($where,$tablename);
		} 
    	$SQL = "UPDATE `" . $this->def . $tablename . "` SET $value WHERE ".$where; 
		
        $this->db->query("set sql_mode=''");
		$return=$this->db->query($SQL);
		return $return;
	}

	function DB_delete_all($tablename, $where, $limit = 'limit 1',$pecial='',$norecycle=''){
		if(!$this->checkTableName($tablename)){
			
			return false;
		}
		if($pecial!=$tablename){
			if(!in_array($tablename,array('temporary_resume','lssave')) && $norecycle!='1'){
				$this->insert_recycle($tablename,$this->site_fetchsql($where,$tablename));
			}
			$SQL = "DELETE FROM `" . $this->def . $tablename . "` WHERE ".$this->site_fetchsql($where,$tablename)." $limit";
		}else{
			if(!in_array($tablename,array('temporary_resume','lssave')) && $norecycle!='1'){
				$this->insert_recycle($tablename,$where);
			}
			$SQL = "DELETE FROM `" . $this->def . $tablename . "` WHERE ".$where." $limit";
		}
		
		$this->db->query("set `sql_mode`=''");
		return $this->db->query($SQL);
	}
	
	function insert_recycle($tablename,$where){
		if(!$this->checkTableName($tablename)){
			
			return false;
		}
		if(!isset($_GET['isdel']) || $_GET['isdel']!="all"){

			$value="tablename='".$tablename."',";
			$value.="ctime='".time()."',";
			if(isset($_SESSION['auid']) && isset($_SESSION['ausername']) && $_SESSION['auid'] && $_SESSION['ausername']){
				$username=$_SESSION['ausername'];
			}else{
				$username= isset($_COOKIE['username']) ? $_COOKIE['username'] : '';
			}
			$value.="username='".$username."',";
			
			$query = $this->db->query("SELECT * FROM `" . $this->def . $tablename . "` WHERE $where");
			$row_del = array();
			while($row=$this->db->fetch_assoc($query)){$row_del[]=$row;}
			
			if(!empty($row_del)){
				foreach($row_del as $delvalue){
					
					$this->DB_insert_once("recycle",$value."body='".serialize($delvalue)."'");
				}
			}
			
		
		}
	}
    
	function DB_query_all($sql,$type='one'){

		$query = $this->db->query($sql);
		if($type=='all'){
			 while($row=$this->db->fetch_array($query)){$return[]=$row;}
		}else{
			$return=$this->db->fetch_array($query);
		}
		return $return;
	}
	
	function DB_select_once($tablename, $where = 1, $select = "*",$special='') {
		if(!$this->checkTableName($tablename)){
			
			return false;
		}
		$cachename=$tablename.$where;
		if(!$return=$this->Memcache_set($cachename)){
			if($this->siteadmindir&&$special==''){
				$where = $this->site_fetchsql($where,$tablename);
			}
			$SQL = "SELECT $select FROM " . $this->def . $tablename . " WHERE ".$where." limit 1";
			$query = $this->db->query($SQL);
			$return=$this->db->fetch_array($query);
			$this->Memcache_set($cachename,$return);
		}
		return $return;
	}


	function member_log($content,$opera='',$type='',$uid=null,$usertype=null){
	    if($_COOKIE['uid'] && $_COOKIE['usertype']){
	        $uid = intval($_COOKIE['uid']);
	        $usertype = intval($_COOKIE['usertype']);
	    }
	    if($uid && $usertype){
	        $value="`uid`='".$uid."',";
	        $value.="`usertype`='".$usertype."',";
			$value.="`content`='".$content."',";
			$value.="`opera`='".$opera."',";
			$value.="`type`='".$type."',";
			$value.="`ip`='".fun_ip_get()."',";
			$value.="`ctime`='".time()."'";
			$this->DB_insert_once("member_log",$value);
		}
	}
    function FormatOptions($Options){
        if(!is_array($Options)){return array('field'=>'*','where'=>'');}
        $WhereStr='';
        if($Options['field']){
            $Field=$Options['field'];
            unset($Options['field']);
        }else{
            $Field='*';
        }
		if($Options['special']){
            $special=$Options['special'];
            unset($Options['special']);
        }
		if($Options['groupby']){
            $WhereStr.=' group by '.$Options['groupby'];
        }
        if($Options['orderby']){
            $WhereStr.=' order by '.$Options['orderby'];
        }

		if($Options['desc']){
            $WhereStr.=" ".$Options['desc'];
        }
        if($Options['limit']){
            $WhereStr.=' limit '.$Options['limit'];
        }
        return array('field'=>$Field,'order'=>$WhereStr,"special"=>$special);
    }
	function FormatWhere($Where){
        $WhereStr='1';
        foreach($Where as $k=>$v){
         
            if(is_numeric($k)){
                if((substr(trim($v),0,3)=='and')||(substr(trim($v),0,2)=='or')){
                    $WhereStr.=' '.$v;
                }elseif($v){
                    $WhereStr.=' and '.$v;
                }
            }else{
				
				if(strpos($k,'<>') > 0){
					$position = strpos($k,'<>');
					$fieldName = trim(substr($k,0,$position));
					$WhereStr .=' and `'.$fieldName.'` <> \''.$v.'\'';
				}
				elseif(strpos($k,'<') > 0){
					$position = strpos($k,'<');
					$fieldName = trim(substr($k,0,$position));
					$WhereStr .=' and `'.$fieldName.'` < \''.$v.'\'';
				}
				elseif(strpos($k,'>') > 0){
					$position = strpos($k,'>');
					$fieldName = trim(substr($k,0,$position));
					$WhereStr .=' and `'.$fieldName.'` > \''.$v.'\'';
				}
				else{
					$WhereStr .=' and `'.$k.'`=\''.$v.'\'';
				}
            }
        }
        return $WhereStr;
    }
	function FormatValues($Values){
        $ValuesStr='';
        foreach($Values as $k=>$v){
          
			if(preg_match("/^[a-zA-Z0-9_]+$/",$k)){
				 if(preg_match('/^[0-9]+$/', $k)){
					$FiledList = @explode(',',$v);
					if(is_array($FiledList)){
						foreach($FiledList as $Fv){
							$FvList = @explode('=',$Fv);
							if($FvList[1]){
									if(strpos($FvList[1],'+') > 0){
										$FiledV = @explode('+',$FvList[1]);
										$ValuesStr.=',`'.str_replace("`",'',$FvList[0]).'`=`'.str_replace("`",'',$FiledV[0]).'`+\''.intval(str_replace("'",'',$FiledV[1])).'\'';
									}
									if(strpos($FvList[1],'-') > 0){
										$FiledV = @explode('-',$FvList[1]);
										$ValuesStr.=',`'.str_replace("`",'',$FvList[0]).'`=`'.str_replace("`",'',$FiledV[0]).'`-\''.intval(str_replace("'",'',$FiledV[1])).'\'';
									}

							}
						}
					}
				}else{
					$ValuesStr.=',`'.$k.'`=\''.$v.'\'';
				}
			}

        }
        return substr($ValuesStr,1);
    }
	function lietou_invtal($uid,$integral,$auto=true,$name="",$pay=true,$pay_state=2,$type="integral",$pay_type=''){
		if($integral!='0'){
			$dingdan=mktime().rand(10000,99999);
			$data['order_id']=$dingdan;
			$data['did']=$this->userdid;
			$data['com_id']=$uid;
			$data['pay_remark']=$name;
			$data['pay_state']=$pay_state;
			$data['pay_time']=time();
			$data['pay_type']=$pay_type;
			if($auto){
				$data['order_price']=$integral;
			}else{$data['order_price']='-'.$integral;}
			if($type=="integral")
			{
				$data['type']=1;
			}else{
				$data['type']=2;
			}
			$id=$this->insert_into("company_pay",$data);
			if($id){
				if($auto){
					$nid=$this->DB_update_all("lt_statis","`$type`=`$type`+$integral","uid='".$uid."'");
				}else{
					$nid=$this->DB_update_all("lt_statis","`$type`=`$type`-$integral","uid='".$uid."'");
				}
				if($nid){
					return $nid;
				}else{
					$this->DB_delete_all("company_pay","`id`='".$id."'");
				}
			}
		}else{
			return true;
		}
	}
    function RemindDeal($TableName,$Values=array(),$Where=array()){
		if(!$this->checkTableName($TableName)){
			
			return false;
		}
        $ValuesStr.=$this->FormatValues($Values);
        $WhereStr='';
        $WhereStr.=$this->FormatWhere($Where);
        $this->DB_update_all($TableName,$ValuesStr,$WhereStr);
    }
    function site_fetchsql($Where,$TableName,$SplitChar=' and '){
		if(!$this->checkTableName($TableName)){
			
			return false;
		}
        if(in_array($TableName,$this->sitetable)){
            if(is_array($Where)&&is_numeric($this->config['did'])){
				$Where['did']=$this->config['did'];
            }else if(is_numeric($this->config['did'])){
				$Where='`did`='.$this->config['did'].$SplitChar.$Where;
            }
        }
        return $Where;
    }

	function get_page($table,$where='',$pageurl='',$limit=20,$field='*',$rowsname='rows'){
		if(!$this->checkTableName($table)){
			
			return false;
		}
		$rows=array();
		$page=$_GET['page']<1?1:$_GET['page'];
		$ststrsql=($page-1)*$limit;
		$num=$this->DB_select_num($table,$where);
		if($num>$limit){
			$pages=ceil($num/$limit);
            $pagenav=Page($page,$num,$limit,$pageurl,$notpl=false,null);
		}
		$rows=$this->DB_select_all($table,$where.' limit '.$ststrsql.','.$limit,$field);
		return array('total'=>$num,'pagenav'=>$pagenav,$rowsname=>$rows);
	}
	function fetch_assoc(){
	    return $this->db->fetch_assoc();
	}
	function checkTableName($table){
	
		 if (preg_match('/^[_a-z]{2,30}$/i',$table)){
		  return true;
		 }else {
		  return false;
		 } 
	
	}
}
?>